#!/usr/bin/env python3
# 保存为 ~/wallpaper_reminder.py

import os
import subprocess
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
import sys
def create_reminder_wallpaper():
    # 壁纸尺寸（根据你的屏幕分辨率调整）
    width, height = 1920, 1080
    
    # 创建新图片
    img = Image.new('RGB', (width, height), color=(30, 30, 60))  # 深蓝色背景
    draw = ImageDraw.Draw(img)
    
    # 尝试加载字体（根据需要调整字体路径）
    try:
        # 确保字体文件存在
        font_path = "/home/w-dingyu01@uos/dy外网/My_model/pygame测试/Ubuntu_18.04_SimHei.ttf"
        m_number = "/home/w-dingyu01@uos/dy外网/My_model/pygame测试/像素.ttf"
        if os.path.exists(font_path):
            font_large = ImageFont.truetype(font_path, 80)
            font_medium = ImageFont.truetype(font_path, 60)
            font_small = ImageFont.truetype(font_path, 25)
        else:
            print(f"字体文件不存在: {font_path}")
            # 如果指定字体不存在，使用系统默认字体
            font_large = ImageFont.load_default()
            font_medium = ImageFont.load_default()
            font_small = ImageFont.load_default()
            # 给默认字体设置固定大小用于计算（需要 Pillow 8.0.0+）
            font_large = ImageFont.load_default().font_variant(size=60)
            font_medium = ImageFont.load_default().font_variant(size=36)
            font_small = ImageFont.load_default().font_variant(size=24)
    except Exception as e:
        print(f"字体加载失败: {e}")
        # 回退到最基础的默认字体
        font_large = ImageFont.load_default()
        font_medium = ImageFont.load_default()
        font_small = ImageFont.load_default()
    
    # 提醒内容
    title = "下班提醒"
    reminder_text = "请记得登记工时和打卡！"
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    steps = ["1. 打开工时系统", "2. 登记今日工作内容", "3. 确认提交", "4. 打卡下班"]
    
    # 绘制标题
    draw.text((width//2, 100), title, fill=(255, 255, 255), font=font_large, anchor="mt")
    
    # 绘制主要提醒文字
    draw.text((width//2, 200), reminder_text, fill=(255, 200, 100), font=font_medium, anchor="mt")
    
    # 绘制步骤
    y_offset = 300
    for i, step in enumerate(steps):
        draw.text((200, y_offset + i * 60), step, fill=(200, 255, 200), font=font_medium)
    
    # 绘制当前时间
    draw.text((width - 400, height - 100), f"最后提醒: {current_time}", 
              fill=(150, 150, 150), font=font_small)
    
    # 保存图片
    wallpaper_path = os.path.expanduser("/home/w-dingyu01@uos/dy外网/My_model/warning/下班提醒壁纸.png")
    img.save(wallpaper_path)
    
    return wallpaper_path

import shutil
def set_wallpaper_deepin(image_path):
    """
    专为 Deepin 桌面环境设置的壁纸函数
    image_path: 壁纸图片的绝对路径
    """
    # 确保文件存在
    if not os.path.isfile(image_path):
        print(f"错误：壁纸文件不存在 - {image_path}")
        return False

    # 构建完整的 file:// URI
    image_uri = f"file://{os.path.abspath(image_path)}"
    
    # 构建 D-Bus 命令
    cmd = [
        'dbus-send',
        '--session',
        '--dest=com.deepin.daemon.Appearance',
        '--print-reply',
        '/com/deepin/daemon/Appearance',
        'com.deepin.daemon.Appearance.SetMonitorBackground',
        'string:"Default"',  # 显示器名称，单显示器通常为 "Default" 或留空 ""
        f'string:"{image_uri}"'
    ]

    try:
        # 执行命令
        result = subprocess.run(cmd, check=True, capture_output=True, text=True, timeout=10)
        print(f"✓ 壁纸已通过 Deepin D-Bus 接口成功设置为: {image_path}")
        return True
    except subprocess.CalledProcessError as e:
        print(f"✗ D-Bus 命令执行失败，返回码: {e.returncode}")
        print(f"错误输出: {e.stderr}")
    except Exception as e:
        print(f"✗ 设置壁纸时发生异常: {e}")
    
    return False
    
if __name__ == "__main__":
    wallpaper_path = create_reminder_wallpaper()
    print(f"壁纸已生成: {wallpaper_path}")
    
    if set_wallpaper_deepin(wallpaper_path):
        print("壁纸设置完成!")
    else:
        print("壁纸设置失败，请手动设置壁纸")